package edu.whu.puppygo.controller;


import edu.whu.puppygo.base.HttpResponse;
import edu.whu.puppygo.dto.PetMatterDto;
import edu.whu.puppygo.dto.PostDto;
import edu.whu.puppygo.entity.PetMatter;
import edu.whu.puppygo.entity.Post;
import edu.whu.puppygo.service.PostService;
import edu.whu.puppygo.util.GlobalUser;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/posts")
public class PostController {
    @Resource
    private PostService postService;

    /**
     * 查询所有帖子
     * @param pageSize
     * @param pageNum
     * @return
     */
    @ApiOperation(value = "查询所有帖子")
    @GetMapping("/getAllPosts")
    public HttpResponse<List<Post>> getAllPosts(int pageSize,int pageNum){
        PageRequest pageRequest = PageRequest.of(pageNum, pageSize);
        List<Post> res = postService.getAllPosts(pageRequest);
        return HttpResponse.success(res);
    }

    /**
     * 查询当前登录用户的所有帖子
     * @param pageSize
     * @param pageNum
     * @return
     */
    @ApiOperation(value = "查询当前登录用户的所有帖子")
    @GetMapping("/getAllPostsByUser")
    public HttpResponse<List<Post>> getAllPostsByUser(int pageSize,int pageNum){
        Long userId = GlobalUser.getUser().getId();
        PageRequest pageRequest = PageRequest.of(pageNum, pageSize);
        List<Post> res = postService.getAllPostsByUserId(userId,pageRequest);
        return HttpResponse.success(res);
    }

    /**
     * 根据用户昵称查询帖子
     * @param nickname
     * @param pageSize
     * @param pageNum
     * @return
     */
    @ApiOperation(value = "根据用户昵称查询帖子")
    @GetMapping("/getAllPostsByNickname")
    public HttpResponse<List<Post>> getAllPostsByNickname(@RequestParam String nickname,
                                                          @RequestParam int pageSize,
                                                          @RequestParam int pageNum){
        PageRequest pageRequest = PageRequest.of(pageNum, pageSize);
        List<Post> res = postService.getAllPostsByNickname(nickname,pageRequest);
        return HttpResponse.success(res);
    }

    /**
     * 根据 userId 查询所有帖子(备用的接口，不知道会不会有用)
     * @param userId
     * @param pageSize
     * @param pageNum
     * @return
     */
    @ApiOperation(value = "根据 userId 查询所有帖子(备用的接口，不知道会不会有用)")
    @GetMapping("/getAllPostsByUserId")
    public HttpResponse<List<Post>> getAllPostsByUserId(@RequestParam Long userId,
                                                        @RequestParam int pageSize,
                                                        @RequestParam int pageNum){
        PageRequest pageRequest = PageRequest.of(pageNum, pageSize);
        List<Post> res = postService.getAllPostsByUserId(userId,pageRequest);
        return HttpResponse.success(res);
    }

    /**
     * 发布帖子
     * @param postDto
     * @return
     */
    @ApiOperation(value = "发布帖子")
    @PostMapping("/publishPost")
    public HttpResponse<Post> publishPost(PostDto postDto,
                                          @RequestParam(value = "file", required = false) MultipartFile mpfile) {
        Post res = postService.publishPost(postDto,mpfile);
        return HttpResponse.success(res);
    }

    /**
     * 更新帖子
     * @param post
     * @return
     */
    @ApiOperation(value = "更新帖子")
    @PutMapping("/updatePost")
    public HttpResponse<Post> updatePost(@RequestBody Post post){
        Post res = postService.updatePost(post);
        return HttpResponse.success(res);
    }

    /**
     * 删除帖子
     * @param postId
     * @return
     */
    @ApiOperation(value = "删除帖子")
    @DeleteMapping("/{postId}")
    public HttpResponse<String> deletePost(@PathVariable Long postId) {
        postService.deletePost(postId);
        return HttpResponse.success("删除成功");
    }
}
